package com.binhai.user;

import com.alibaba.fastjson.JSON;
import com.binhai.db.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserDao {
    @Autowired
    private SpringDB springDB;
    public List getAllInfos() {
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "select * from t_user where  state=0";
            //使用模版查询，返回list列表
            List list = jdbcTemplate.queryForList(sql);
            //响应数据
            return list;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public Map login(UserBean bean){
        JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
//        String sql1 = "select * from t_user where  state=0";
//        List list1 = jdbcTemplate.queryForList(sql1);
//        System.out.println(list1);
    System.out.println(bean.getName());

        String sql = "select * from t_user where name = '" + bean.getName() + "' and pass = '" + bean.getPass() + "' and state=0";
        //使用模版查询，返回list列表
        List list = jdbcTemplate.queryForList(sql);
        Map map = new HashMap();
        if (list.size()>0){
            map.put("result", "success");
        }else{
            map.put("result", "error");
        }
        //map转成JSON字符串
        String s = JSON.toJSONString(map);
        //响应数据
        return map;
    }
}
